package cn.max.plat.utils;

import cn.max.git.GitUtils;
import com.mongodb.DBObject;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;

import java.io.File;

/**
 * Created by Max-Goday on 15/7/24.
 */
public class AntHelper {


	public static boolean depoly(String repo_name, DBObject server) {

		System.out.println(" >>>>>>> START TO BUILD >>>>>>");

		File file = GitUtils.getAntBuildConfigFile(repo_name);

		Project project = new Project();

		try {
			project.fireBuildStarted();
			project.init();
			ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
			projectHelper.parse(project, file);

			project.setProperty("server.ip", String.valueOf(server.get("ip")));
			project.setProperty("server.port", String.valueOf(server.get("port")));
			project.setProperty("server.username", String.valueOf(server.get("login_name")));
			project.setProperty("server.password", String.valueOf(server.get("login_password")));
			project.setProperty("server.deploydir", project.getProperty("server.username") + "@" + project.getProperty("server.ip") + ":" + String.valueOf(server.get("deploy_dir")));

			project.executeTarget(project.getDefaultTarget()); //调用默认的target标签

			project.fireBuildFinished(null);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(" >>>>>>> BUILD FAILED >>>>>>");
			return false;
		}

		System.out.println(" >>>>>>> BUILD DONE >>>>>>");

		return true;

	}

}
